<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/15 14:00
 * Modified Time: 2019/7/15 14:00
 * Version: 2.0
 */

namespace App\Modules\MemberCard\Criterias;


use App\Lib\Facades\FLAuth;
use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class GetMyMemberCardListCriteria extends BaseCriterias
{

    public function apply($model, RepositoryInterface $repository)
    {
        $member_id = FLAuth::id();

        $select = [
            'hx_member_card.id',
            'hx_card_type.name',
            'hx_store.name as store_name',
            'IF(hx_card_type.category=1,"次数卡","充值卡") as card_category',
            'DATE_FORMAT(hx_member_card.created_at,"%Y/%m/%d") as created_time'
        ];

        $select = implode($select, ',');
        $model = $model->selectRaw($select)
            ->where('member_id',$member_id)
            ->where('member_card.status',1)
            ->leftJoin('store', 'member_card.store_id', 'store.id')
            ->leftJoin('card_type', 'member_card.type_id', 'card_type.id')
            ->orderBy('id', 'DESC');

        return $model;
    }
}